***Analyzing YouGov responses to party identification question***
clear
postutil clear
use "whypid_yougov.dta"

g leaner = pid7 == 3 | pid7 == 5
g all = 1
g excludingnones = none == 0
g strongdem = pid7 == 1
g weakdem = pid7 == 2
g leandem = pid7 == 3
g independent = pid7 == 4
g leanrep = pid7 == 5
g weakrep = pid7 == 6
g strongrep = pid7 == 7
g partisan = independent == 0
g excludingleanders = independent == 0 & leaner == 0
g strongpartisan = pid7 == 1 | pid7 == 7
g dem = pid7 < 4
g rep = pid7 > 4
g onecategory = policy + disdain + candidates + performance + socialization + groups == 1
g think_when = think_vs_are*when_asked_first


*Number and share uncategorized, mentioned in text
tab none
table independent, statistic(mean none)

*Table 2
postfile Table policy disdain candidates socialization performance groups none samplesize using "Table.dta", replace
foreach j in all excludingnones strongdem weakdem leandem independent leanrep weakrep strongrep partisan excludingleanders strongpartisan dem rep onecategory {
	preserve
	keep if `j' == 1
	foreach i in policy disdain candidates socialization performance groups none {
		sum `i' [aw = weight]
		scalar _`i' = r(mean)
	}
	post Table (_policy) (_disdain) (_candidates) (_socialization) (_performance) (_groups) (_none) (r(N)) 
	restore
}
postclose Table
	preserve
	clear
	use "Table.dta"
	foreach i in policy disdain candidates performance socialization groups none {
		tostring `i', format(%9.3f) replace force
		replace `i' = subinstr(`i', "0.", ".", .)
	}
	save "Table2.dta", replace
	restore


*row 1 of Table 3
*Extract the excluding leaners weighted means
preserve
keep if independent == 0 & leaner == 0 & none == 0
tabstat policy disdain candidates socialization performance groups [aw = weight], format(%9.3f)
collapse policy disdain candidates socialization performance groups [aw = weight]
g row = "MassResults"
order row
save "MassResultsForTable3.dta", replace
restore


*Table 4
reg policy think_vs_are when_asked_first [aw = weight], r
outreg2 using "questionwording.txt", 2aster dec(3) replace
foreach i in disdain candidates socialization performance groups { 
reg `i' think_vs_are when_asked_first [aw = weight], r
outreg2 using "questionwording.txt", 2aster dec(3) append
}
*test for interactions (mentioned in text)
foreach i in policy disdain candidates socialization performance groups { 
reg `i' think_vs_are when_asked_first think_when [aw = weight], r
}

*Table A4
postfile Table policy disdain candidates socialization performance groups none samplesize using "Table.dta", replace
foreach j in all excludingnones strongdem weakdem leandem independent leanrep weakrep strongrep partisan excludingleanders strongpartisan dem rep onecategory {
	preserve
	keep if `j' == 1
	foreach i in policy disdain candidates socialization performance groups none {
		sum `i'
		scalar _`i' = r(mean)
	}
	post Table (_policy) (_disdain) (_candidates) (_socialization) (_performance) (_groups) (_none) (r(N)) 
	restore
}
postclose Table
	preserve
	clear
	use "Table.dta"
	foreach i in policy disdain candidates performance socialization groups none {
		tostring `i', format(%9.3f) replace force
		replace `i' = subinstr(`i', "0.", ".", .)
	}
	save "TableA4.dta", replace
	restore

	
*Table A5
drop if independent == 1
drop if none == 1
reg policy dem strongpartisan extreme woman white age40 employ college income70 northeast midwest west [aw = weight], r
outreg2 using "heterogeneity.txt", 2aster dec(3) replace
foreach i in disdain candidates socialization performance groups {
reg `i' dem strongpartisan extreme woman white age40 employ college income70 northeast midwest west [aw = weight], r
outreg2 using "heterogeneity.txt", 2aster dec(3) append
}
	

***Table 3***
clear
use "ExpertSurveyAnonymous.dta"
rename whypid_1 policy
rename whypid_2 candidates
rename whypid_3 performance
rename whypid_4 socialization
rename whypid_5 groups
rename whypid_6 disdain
order policy disdain candidates performance socialization groups
collapse policy disdain candidates performance socialization groups
foreach i of varlist policy disdain candidates performance socialization groups {
replace `i' = `i'/100
}
g row = "ExpertPredictions"
order row
save "ExpertPredictions.dta", replace

clear
use "ExpertSurveyAnonymous.dta"
rename whypidreally_1 policy
rename whypidreally_2 candidates
rename whypidreally_3 performance
rename whypidreally_4 socialization
rename whypidreally_5 groups
rename whypidreally_6 disdain
order policy disdain candidates performance socialization groups
collapse policy disdain candidates performance socialization groups
foreach i of varlist policy disdain candidates performance socialization groups {
replace `i' = `i'/100
}
g row = "ExpertAssessments"
order row
save "ExpertAssessments.dta", replace

clear
use "MassResultsForTable3.dta"
append using "ExpertPredictions.dta"
append using "ExpertAssessments.dta"
foreach i in policy disdain candidates performance socialization groups {
tostring `i', replace format(%9.3f) force
}
save "Table3.dta", replace


***Analyzing mass responses to partisan animosity question***
clear
use "whyFT.dta"

g leaner = PID7 == "Dem Lean" | PID7 == "Rep Lean"
g strongpartisan = PID7 == "Strong Dem" | PID7 == "Strong Rep"
g dem = PID7 == "Dem Lean" | PID7 == "Strong Dem"  | PID7 == "Weak Dem"

*assess agreement, discussed in Appendix text
preserve
keep tot_m_values-tot_m_rejectpremise id
reshape long tot_m_, i(id) j(category) string
tab tot_m_
replace tot_m_ = 4 if tot_m_ == 0
replace tot_m_ = 3 if tot_m_ == 1
replace tot_m_ = -1*tot_m_
tab tot_m_
restore

g totalcats = 0
foreach i in values specific retrospective origins group reflectdisdain chartraits factual rejectpremise {
g `i' = tot_m_`i' >= 3
replace totalcats = totalcats + 1 if `i' == 1
}
g none = totalcats == 0
rename values policy
rename specific candidates
rename retrospective performance
rename origins experience
rename group groups
rename reflectdisdain disdain
rename chartraits character
rename rejectpremise premise
g like50 = pid_ft_value >= 50

*Table 5
tabstat policy character groups candidates experience performance factual disdain premise none, format(%9.3f)
count
tabstat policy character groups candidates experience performance factual disdain premise none if like50 == 0, format(%9.3f)
count if like50 == 0
tabstat policy character groups candidates experience performance factual disdain premise none if none == 0, format(%9.3f)
count if none == 0
tabstat policy character groups candidates experience performance factual disdain premise none if premise == 0, format(%9.3f)
count if premise == 0
tabstat policy character groups candidates experience performance factual disdain premise none if leaner == 0, format(%9.3f)
count if leaner == 0
tabstat policy character groups candidates experience performance factual disdain premise none if strongpartisan == 1, format(%9.3f)
count if strongpartisan == 1
tabstat policy character groups candidates experience performance factual disdain premise none if totalcats == 1, format(%9.3f)
count if totalcats == 1

*Column 1 of Table 6
tabstat policy character groups candidates experience performance factual disdain premise none if leaner == 0 & none == 0, format(%9.3f)

*Table A6
drop if none == 1
reg policy dem strongpartisan extreme female white old employed college highincome northeast midwest west, r
outreg2 using "heterogeneity_FT.txt", 2aster dec(3) replace
reg character dem strongpartisan extreme female white old employed college highincome northeast midwest west, r
outreg2 using "heterogeneity_FT.txt", 2aster dec(3) append
reg groups dem strongpartisan extreme female white old employed college highincome northeast midwest west, r
outreg2 using "heterogeneity_FT.txt", 2aster dec(3) append
reg candidates dem strongpartisan extreme female white old employed college highincome northeast midwest west, r
outreg2 using "heterogeneity_FT.txt", 2aster dec(3) append
reg experience dem strongpartisan extreme female white old employed college highincome northeast midwest west, r
outreg2 using "heterogeneity_FT.txt", 2aster dec(3) append
reg performance dem strongpartisan extreme female white old employed college highincome northeast midwest west, r
outreg2 using "heterogeneity_FT.txt", 2aster dec(3) append
reg factual dem strongpartisan extreme female white old employed college highincome northeast midwest west, r
outreg2 using "heterogeneity_FT.txt", 2aster dec(3) append
reg disdain dem strongpartisan extreme female white old employed college highincome northeast midwest west, r
outreg2 using "heterogeneity_FT.txt", 2aster dec(3) append
reg premise dem strongpartisan extreme female white old employed college highincome northeast midwest west, r
outreg2 using "heterogeneity_FT.txt", 2aster dec(3) append



***Table 3***
*Column 2
clear
use "ExpertSurveyAnonymous.dta"
rename whyft_1 policy
rename whyft_2 candidates
rename whyft_3 performance
rename whyft_4 experience
rename whyft_5 groups
rename whyft_8 disdain
rename whyft_9 character
rename whyft_10 factual
rename whyft_11 premise
foreach i of varlist policy character groups candidates experience performance factual disdain premise {
replace `i' = `i'/100
}
tabstat policy character groups candidates experience performance factual disdain premise, format(%9.3f)
*Column 3
clear
use "ExpertSurveyAnonymous.dta"
rename whyftreally_1 policy
rename whyftreally_2 candidates
rename whyftreally_3 performance
rename whyftreally_4 experience
rename whyftreally_5 groups
rename whyftreally_8 disdain
rename whyftreally_9 character
rename whyftreally_10 factual
rename whyftreally_11 premise
foreach i of varlist policy character groups candidates experience performance factual disdain premise {
replace `i' = `i'/100
}
tabstat policy character groups candidates experience performance factual disdain premise, format(%9.3f)


***Analyzing handcoded responses to PID question, Prolific survey
clear
use "whypid_prolific.dta"

*rates of agreement, discussed in Appendix
preserve
keep tot* id
reshape long tot, i(id) j(category) string
tab tot
g disagreement = tot
replace disagreement = 1 if tot == 3
replace disagreement = 0 if tot == 4
tab disagreement
restore

*types of uncategorized responses, discussed in Appendix
tab nonetype

*Table A1
g leaner = pid7 == 3 | pid7 == 5
g all = 1
g strongdem = pid7 == 1
g weakdem = pid7 == 2
g leandem = pid7 == 3
g independent = pid7 == 4
g leanrep = pid7 == 5
g weakrep = pid7 == 6
g strongrep = pid7 == 7
g partisan = independent == 0
g excludingleanders = independent == 0 & leaner == 0
g strongpartisan = pid7 == 1 | pid7 == 7
g dem = pid7 < 4
g rep = pid7 > 4
g onecategory = policy + disdain + candidates + performance + socialization + groups == 1
g excludinguncategorized = none == 0
postfile Table policy disdain candidates performance socialization groups none samplesize using "Table.dta", replace
foreach j in all excludinguncategorized strongdem weakdem leandem independent leanrep weakrep strongrep partisan excludingleanders strongpartisan dem rep onecategory {
	preserve
	keep if `j' == 1
	foreach i in policy disdain candidates socialization performance groups none {
		sum `i'
		scalar _`i' = r(mean)
	}
	post Table (_policy) (_disdain) (_candidates) (_socialization) (_performance)  (_groups) (_none) (r(N)) 
	restore
}
postclose Table
clear
use "Table.dta"
foreach i in policy disdain candidates socialization performance  groups none {
	tostring `i', format(%9.3f) replace force
	replace `i' = subinstr(`i', "0.", ".", .)
	}
save "Table A1.dta", replace
	

***Comparing GPT and human codings***
clear
use "HandCodedSample.dta"

*Table A2
foreach i in policy disdain candidates performance socialization groups {
sum *_`i'
}

*Table A3
reshape long gpt_ af_ gh_ rj_ lo_, i(yougovid) j(category) string
foreach j in gpt af gh rj lo {
rename `j'_ `j'
}
corr gpt-lo


